<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="资产ID，主键" prop="assetId">
                        <el-input v-model.number="dataForm.assetId" placeholder="资产ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="资产类型（如股票、债券、基金等）" prop="assetType">
                        <el-input v-model="dataForm.assetType" placeholder="资产类型（如股票、债券、基金等）"></el-input>
                </el-form-item>
                <el-form-item label="资产名称" prop="assetName">
                        <el-input v-model="dataForm.assetName" placeholder="资产名称"></el-input>
                </el-form-item>
                <el-form-item label="资产代码" prop="assetCode">
                        <el-input v-model="dataForm.assetCode" placeholder="资产代码"></el-input>
                </el-form-item>
                <el-form-item label="购买日期" prop="purchaseDate">
                        <el-date-picker
                                v-model="dataForm.purchaseDate"
                                type="datetime"
                                placeholder="购买日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="购买价格" prop="purchasePrice">
                        <el-input v-model="dataForm.purchasePrice" placeholder="购买价格"></el-input>
                </el-form-item>
                <el-form-item label="持有数量" prop="quantity">
                        <el-input v-model="dataForm.quantity" placeholder="持有数量"></el-input>
                </el-form-item>
                <el-form-item label="持有成本" prop="holdingCost">
                        <el-input v-model="dataForm.holdingCost" placeholder="持有成本"></el-input>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createTime">
                        <el-date-picker
                                v-model="dataForm.createTime"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updateTime">
                        <el-date-picker
                                v-model="dataForm.updateTime"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    assetId: null,
                    assetType: null,
                    assetName: null,
                    assetCode: null,
                    purchaseDate: null,
                    purchasePrice: null,
                    quantity: null,
                    holdingCost: null,
                    createTime: null,
                    updateTime: null,
                },
                dataRule: {
                    assetId: [
                        { required: true, message: '资产ID，主键不能为空', trigger: 'blur' }
                    ],
                    assetType: [
                        { required: true, message: '资产类型（如股票、债券、基金等）不能为空', trigger: 'blur' }
                    ],
                    assetName: [
                        { required: true, message: '资产名称不能为空', trigger: 'blur' }
                    ],
                    assetCode: [
                        { required: true, message: '资产代码不能为空', trigger: 'blur' }
                    ],
                    purchaseDate: [
                        { required: true, message: '购买日期不能为空', trigger: 'blur' }
                    ],
                    purchasePrice: [
                        { required: true, message: '购买价格不能为空', trigger: 'blur' }
                    ],
                    quantity: [
                        { required: true, message: '持有数量不能为空', trigger: 'blur' }
                    ],
                    holdingCost: [
                        { required: true, message: '持有成本不能为空', trigger: 'blur' }
                    ],
                    createTime: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
                    ],
                    updateTime: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppAssetInfo/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appassetinfo
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppAssetInfo/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>